package com.lyl.bookstore.user.web.servlet;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lyl.bookstore.user.domain.User;
import com.lyl.bookstore.user.service.UserException;
import com.lyl.bookstore.user.service.UserService;

import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
import cn.itcast.servlet.BaseServlet;
import cn.itcast.utils.CommonUtils;
/**
 * User表述层
 */
@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;

	private UserService userService = new UserService();


	public String regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//一键获取form
		User form = CommonUtils.toBean(request.getParameterMap(), User.class);
		
		//校验用户名、密码和email，存储到一个map中
		Map<String,String> errors = new HashMap<String,String>();
		String username = form.getUsername();
		String password = form.getPassword();
		String email = form.getEmail();
		//用户名校验
		if(username.isEmpty() || username.trim().length() <3 || username.trim().length() > 10) {
			errors.put("username", "用户名格式错误，请输入一个3-10个字符");
		}
		//密码校验
		if(password.isEmpty() || password.trim().length() < 3 || username.trim().length() > 10) {
			errors.put("password", "密码格式错误，请输入一个3-10个长度的字符");
		}
		//email校验
		if(email.isEmpty() || !email.matches("\\w+@\\w+\\.\\w+")) {
			errors.put("email", "邮箱格式错误！");
		}
		//判断map是否为空
		if(errors.size() > 0) {
			request.setAttribute("errors", errors);
			request.setAttribute("user", form);
			return "f:/jsps/user/regist.jsp";
		}
		
		//补全form信息
		form.setUid(CommonUtils.uuid());
		form.setCode(CommonUtils.uuid() + CommonUtils.uuid());
		//去service注册
		try {
			userService.regist(form);
		} catch (UserException e) {
			// TODO Auto-generated catch block
			request.setAttribute("msg", e.getMessage());
			request.setAttribute("user", form);
		}
		
		//发送邮件
		Properties props = new Properties();
		props.load(this.getClass().getClassLoader().getResourceAsStream("email"
				+ "_template.properties"));
		String host = props.getProperty("host");//主机名
		String uname = props.getProperty("uname");
		String pwd = props.getProperty("pwd");
		String from = props.getProperty("from");
		String to = form.getEmail();
		String subject = props.getProperty("subject");
		String content = props.getProperty("content");
		content = MessageFormat.format(content, form.getCode());
		
		Session session = MailUtils.createSession(host, uname, pwd);
		Mail mail = new Mail(from, to, subject, content);
		
		try {
			MailUtils.send(session, mail);
		} catch (MessagingException e) {
			throw new RuntimeException();
		}
		
		//保存成功信息转发到msg.jsp
		request.setAttribute("msg", "注册成功，请到<a href='mail."
				+ form.getEmail().split("@")[1] + "'>邮箱</a>激活！");
		return "f:/jsps/msg.jsp";
	}

}
